using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Rapier.Kodachi.Core
{
    public partial class AddEditIndexFolder : Form
    {
        private IndexFolder _IndexFolder;
        public IndexFolder IndexFolder
        {
            get { return _IndexFolder; }
            set { _IndexFolder = value; }
        }

        public AddEditIndexFolder() : this(new IndexFolder())
        {
            Text = "Add Index Folder";
            IndexFolder.IsSpecialFolder = true;
        }

        public AddEditIndexFolder(IndexFolder indexFolder)
        {
            InitializeComponent();

            cbSystemFolder.DataBindings.Add("Enabled", rbSystemFolder, "Checked");
            tbCustomFolder.DataBindings.Add("Enabled", rbCustomFolder, "Checked");
            buttonBrowse.DataBindings.Add("Enabled", rbCustomFolder, "Checked");

            _IndexFolder = indexFolder;
            tbCustomFolder.DataBindings.Add("Text", _IndexFolder, "Path");
            Binding binding = new Binding("SelectedIndex", _IndexFolder, "SpecialFolder");
            binding.Format += new ConvertEventHandler(binding_Format);
            binding.Parse += new ConvertEventHandler(binding_Parse);
            cbSystemFolder.DataBindings.Add(binding);
            listTextExtensions.DataSource = _IndexFolder.Extensions;
            rbSystemFolder.DataBindings.Add("Checked", _IndexFolder, "IsSpecialFolder");
        }

        #region Event Handlers
        private void buttonOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void binding_Parse(object sender, ConvertEventArgs e)
        {
            switch((int)e.Value)
            {
                case 0:
                    e.Value = (int)Environment.SpecialFolder.StartMenu;
                    break;
                case 1:
                    e.Value = (int)Environment.SpecialFolder.MyDocuments;
                    break;
                case 2:
                    e.Value = (int)Environment.SpecialFolder.Desktop;
                    break;
                case 3:
                    e.Value = (int)Environment.SpecialFolder.Favorites;
                    break;
            }
        }

        private void binding_Format(object sender, ConvertEventArgs e)
        {
            switch ((int)e.Value)
            {
                case (int)Environment.SpecialFolder.StartMenu:
                    e.Value = 0;
                    break;
                case (int)Environment.SpecialFolder.MyDocuments:
                    e.Value = 1;
                    break;
                case (int)Environment.SpecialFolder.Desktop:
                    e.Value = 2;
                    break;
                case (int)Environment.SpecialFolder.Favorites:
                    e.Value = 3;
                    break;
            }
        }

        private void buttonAddExtension_Click(object sender, EventArgs e)
        {
            GetTextExtension gte = new GetTextExtension();
            if (gte.ShowDialog() == DialogResult.Cancel)
                return;

            IndexExtension ext = new IndexExtension(gte.Extension);
            if (_IndexFolder.Extensions.Contains(ext))
            {
                MessageBox.Show("This extension is already being indexed. It will not be added to the list.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            _IndexFolder.Extensions.Add(new IndexExtension(gte.Extension));
        }

        private void buttonRemoveExtension_Click(object sender, EventArgs e)
        {
            if (listTextExtensions.SelectedIndex == -1)
            {
                MessageBox.Show("Please select an extension from the list on the left first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            _IndexFolder.Extensions.RemoveAt(listTextExtensions.SelectedIndex);
        }
        #endregion

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.Cancel)
                return;

            IndexFolder.Path = fbd.SelectedPath;
            tbCustomFolder.Text = IndexFolder.Path;
        }
    }
}